//
//  MQNavigationViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/10/21.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQNavigationViewController.h"

@interface MQNavigationViewController ()<UIGestureRecognizerDelegate>

@end

@implementation MQNavigationViewController

/**
 * 当第一次使用这个类的时候会调用一次
 */
+ (void)initialize
{
    // 当导航栏用在XMGNavigationController中, appearance设置才会生效
    //    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:[self class], nil];
//    DSCQCustomFont *customfont = [DSCQCustomFont shareInstance];
    
    
    UINavigationBar *bar = [UINavigationBar appearance];
    bar.backgroundColor = BSTColorFromRGB(0xff8e01);
    
    
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, BSTColorFromRGB(0xff8e01).CGColor);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    //设置导航栏背景图  也可以自己用颜色画一张背景图
    [bar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    
    [bar setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:16],
                                  NSForegroundColorAttributeName : BSTColorFromRGB(0xffffff)}];
    
    // 设置item
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    // UIControlStateNormal
    NSMutableDictionary *itemAttrs = [NSMutableDictionary dictionary];
    itemAttrs[NSForegroundColorAttributeName] = BSTColorFromRGB(0x333333);
    itemAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    [item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
    // UIControlStateDisabled
    NSMutableDictionary *itemDisabledAttrs = [NSMutableDictionary dictionary];
    itemDisabledAttrs[NSForegroundColorAttributeName] = BSTColorFromRGB(0x333333);
    [item setTitleTextAttributes:itemDisabledAttrs forState:UIControlStateDisabled];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.interactivePopGestureRecognizer.delegate = self;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    
    if (self.childViewControllers.count == 1) {
        
        return NO;
    }
    return YES;
}



/**
 * 可以在这个方法中拦截所有push进来的控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (self.childViewControllers.count > 0) { // 如果push进来的不是第一个控制器
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置 “返回“  标题
        //        [button setTitle:@"返回" forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"delu_return"] forState:UIControlStateNormal]; //默认状态下的返回图
        [button setImage:[UIImage imageNamed:@"delu_return"] forState:UIControlStateHighlighted]; //高亮状态下的返回图
        
        
//        [button setTitle:@" 返回" forState:UIControlStateNormal];
        [button setTitleColor:BSTColorFromRGB(0x333333) forState:UIControlStateNormal];
        button.bst_size = CGSizeMake(55, 30);
        // 让按钮内部的所有内容左对齐
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //        [button sizeToFit];
        
        // 让按钮的内容往左边偏移10
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
        // 修改导航栏左边的item
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        
        viewController.navigationController.navigationBar.backgroundColor = [UIColor grayColor];
        
        viewController.hidesBottomBarWhenPushed = YES;
        
    }
    // 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
    [super pushViewController:viewController animated:animated];
    
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}


@end
